// 使用vuex管理路由规则是为了sidebar视图正常展示菜单
import { constantRoutes, asyncRoutes } from '@/router/index'
export default {
  state: {
    routes: constantRoutes // vuex管理的路由规则
  },
  mutations: {
    // 设置新的路由规则
    setRoutes(state, filterRoutes) {
      state.routes = [...constantRoutes, ...filterRoutes, { path: '*', redirect: '/404', hidden: true }]
    },
    // 重置路由规则
    resetRoutes(state) {
      state.routes = [...constantRoutes, { path: '*', redirect: '/404', hidden: true }]
    }
  },
  actions: {
    // 将过滤的权限路由抽离进来
    getFilterRoutes(context, menus) {
      const filterRoutes = asyncRoutes.filter(item => menus.includes(item.children[0].name))
      context.commit('setRoutes', filterRoutes)
      return filterRoutes // 此处return 这个结果是为了addRoutes添加规则使用
    }
  },
  namespaced: true
}
